/**
 * 
 */
package com.netscenery.yang.model;

import org.jdom2.Attribute;
import org.jdom2.Element;

import com.netscenery.util.XMLTag;
import com.netscenery.yang.YangKeyword;
import com.netscenery.yang.YangStatement;
import com.netscenery.yang.designer.MapInfo;
import com.netscenery.yang.parser.YangParserException;

/**
 * @author yanzi
 *
 */
public class Must extends ModelElement {
	private String description;
	private String reference;
	private String error_app_tag;
	private String error_message;
	/**
	 * 
	 */
	public Must() {
		// TODO Auto-generated constructor stub
	}
	public boolean checkName(String name){
		//TODO:以后实现xpath表达式解析
		return true;
	}
	/**
	 * @param env
	 * @param name
	 * @throws ModelException 
	 */
	public Must(Env env, String name) throws ModelException {
		super(env, name);
		// TODO Auto-generated constructor stub
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getReference() {
		return reference;
	}

	public void setReference(String reference) {
		this.reference = reference;
	}

	public String getError_app_tag() {
		return error_app_tag;
	}

	public void setError_app_tag(String error_app_tag) {
		this.error_app_tag = error_app_tag;
	}

	public String getError_message() {
		return error_message;
	}

	public void setError_message(String error_message) {
		this.error_message = error_message;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#toYang()
	 */
	@Override
	public YangStatement toYang() {
		YangStatement statement = new YangStatement(YangKeyword.MUST.getKeyword(),this.name);
		
		if (!isNullString(this.description)){
			YangStatement descr = new YangStatement(YangKeyword.DESCRIPTION.getKeyword(),this.description);
			statement.addChild(descr);
		}
		if (!isNullString(this.reference)){
			YangStatement ref = new YangStatement(YangStatement.YANG_STATEMENT_REFERENCE,this.getReference());
			statement.addChild(ref);
		}
		if (!isNullString(this.error_app_tag)){
			YangStatement tag = new YangStatement(YangKeyword.ERRORAPPTAG.getKeyword(),this.error_app_tag);
			statement.addChild(tag);
		}
		if (!isNullString(this.error_message)){
			YangStatement ref = new YangStatement(YangKeyword.ERRORMESSAGE.getKeyword(),this.error_message);
			statement.addChild(ref);
		}
		return statement;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#toYin()
	 */
	@Override
	public Element toYin() {
		Element element = new Element(YangKeyword.MUST.getKeyword(),env.getNs());
		Attribute con = new Attribute("condition",this.name);
		element.setAttribute(con);
		if (!isNullString(this.description)){
			Element descr = new Element(YangStatement.YANG_STATEMENT_DESCRIPTION,env.getNs());
			Element text = new Element(XMLTag.XML_TAG_TEXT,env.getNs());
			text.setText(this.description);
			descr.addContent(text);
			element.addContent(descr);
		}
		if (!isNullString(this.reference)){
			Element ref = new Element(YangStatement.YANG_STATEMENT_REFERENCE,env.getNs());
			Element text = new Element(XMLTag.XML_TAG_TEXT,env.getNs());
			text.setText(this.reference);
			ref.addContent(text);
			element.addContent(ref);
		}
		
		if (!isNullString(this.error_app_tag)){
			Element tag = new Element(YangKeyword.ERRORAPPTAG.getKeyword(),env.getNs());
			Attribute value = new Attribute(XMLTag.XML_TAG_VALUE,this.error_app_tag);
			tag.setAttribute(value);
			element.addContent(tag);
		}
		if (!isNullString(this.error_message)){
			Element message = new Element(YangStatement.YANG_STATEMENT_REFERENCE,env.getNs());
			Element value = new Element(XMLTag.XML_TAG_VALUE,env.getNs());
			value.setText(this.error_message);
			message.addContent(value);
			element.addContent(message);
		}
		
		return element;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#toXML()
	 */
	@Override
	public Element toXML() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#readXML(org.jdom2.Element)
	 */
	@Override
	public int readXML(Element element) throws ModelException {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#readYin(org.jdom2.Element)
	 */
	@Override
	public int readYin(Element statement) throws ModelException {
		if (null == statement){
			return MODELELEMENT_OK;
		}

		// 描述
		if (statement.getName()
				.equals(YangStatement.YANG_STATEMENT_DESCRIPTION)) {
			Element text = statement.getChild(XMLTag.XML_TAG_TEXT, env.getNs());
			if (null == text) {
				return MODELELEMENT_FAIL;
			}
			this.setDescription(text.getText());
		}

		// reference
		else if (statement.getName().equals(
				YangStatement.YANG_STATEMENT_REFERENCE)) {

			Element text = statement.getChild(XMLTag.XML_TAG_TEXT, env.getNs());
			if (null == text) {
				return MODELELEMENT_FAIL;
			}
			this.setReference(text.getText());
		}
		// error-app-tag
		else if (statement.getName().equals(
				YangKeyword.ERRORAPPTAG.getKeyword())) {
			this.setError_app_tag(statement.getAttributeValue(XMLTag.XML_TAG_VALUE, env.getNs()));
		}
		// error-message
		else if (statement.getName().equals(
				YangKeyword.ERRORMESSAGE.getKeyword())) {

			Element value = statement.getChild(XMLTag.XML_TAG_VALUE, env.getNs());
			if (null == value) {
				return MODELELEMENT_FAIL;
			}
			this.setError_message(value.getText());
		}
		return MODELELEMENT_OK;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#readYANG(com.netscenery.yang.YangStatement)
	 */
	@Override
	public int readYANG(YangStatement statement) throws YangParserException {
		if (null == statement){
			return MODELELEMENT_OK;
		}
		String value = statement.getArgument();
		
		//描述
		if (statement.getKeyword().equals(YangStatement.YANG_STATEMENT_DESCRIPTION)){
			this.setDescription(value);
		}
	    //reference
		else if (statement.getKeyword().equals(YangStatement.YANG_STATEMENT_REFERENCE)){
			this.setReference(value);
		}
	  
	    //error-app-tag
		else if (statement.getKeyword().equals(YangKeyword.ERRORAPPTAG.getKeyword())){
			this.setError_app_tag(value);
		}	
	    //error-message
		else if (statement.getKeyword().equals(YangKeyword.ERRORMESSAGE.getKeyword())){
			this.setError_message(value);
		}	
		return MODELELEMENT_OK;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#isFieldMandatory(java.lang.String)
	 */
	@Override
	public boolean isFieldMandatory(String field) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#getMapInfo()
	 */
	@Override
	public MapInfo getMapInfo() throws SecurityException, NoSuchMethodException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update() {
		// TODO Auto-generated method stub
		
	}

}
